<?php


/**
 * 
 * 根据话题抓取微博数据
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}


/*ini_set("display_errors",ture);
error_reporting(E_ALL);*/

define("CRON_NAME","qq_v_tag_");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
//$_GET["api_debug"] = true;
echo "<pre>";
//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/qq/inc.php");


class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		global $config;
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$mblog_from_api = new mblog_from_api();
		$db_cron_get_manage = new db_cron_get_manage();
		foreach($config["site_name"] as $site_id => $site_name)
		{
			if($site_name == "总站")continue;
			
			
			
			$get_key = CRON_NAME.$site_name;
			$get_res = $db_cron_get_manage->cron_get_item_by_key($get_key);
			$cron_start_time = $get_res["get_num"];
			$cron_end_time = $get_res["get_num_end"];
			
			//正抓
			$this->get_list_by_topic($site_name,$site_id,1,$cron_start_time,$black_word,$get_key,$get_type=1);
			//反抓
			$this->get_list_by_topic($site_name,$site_id,1,$cron_end_time,$black_word,$get_key,$get_type=2);
				
			//如果有别名
			$topic_alias = $config["site_alias"][$site_id] ;
			if($topic_alias)
			{
				foreach($topic_alias as $alias)
				{
					$get_key = CRON_NAME.$alias;
					$get_res = $db_cron_get_manage->cron_get_item_by_key($get_key);
					$cron_start_time = $get_res["get_num"];
					$cron_end_time = $get_res["get_num_end"];
					
					//正抓
					$this->get_list_by_topic($alias,$site_id,1,$cron_start_time,$black_word,$get_key,$get_type=1);
					//反抓
					$this->get_list_by_topic($alias,$site_id,1,$cron_end_time,$black_word,$get_key,$get_type=2);
					
				}
				
			}
			
		}
		
		
	}	
	
	
	
	//根据话题抓取数据并入库
	function get_list_by_topic($topic,$id,$p=1,$starttime="",$black_word="",$get_key="",$get_type="")
	{
		global $config;
		

		$mblog_from_api = new mblog_from_api();
					
		$mblog_get_mblog_from_qq = new mblog_get_mblog_from_qq();
		$pcount= 30;
		$max_time = "";
		$min_time = "";
		//执行时间超过30分钟，退出
		if(time() - CRON_START_TIME > 900)
		{
			exit;
		}
		$db_base = new db_base();
		$mblog_from_api = new mblog_from_api();
		$user_info = new user_info();
		$db_mblog_video_content = new db_mblog_video_content();
		
		
		if($topic)
		{
			//如果$get_type==2表明是倒抓，无视
			if($get_type == 2)
			{
				$mblog_list = $mblog_get_mblog_from_qq->get_mblog_list_by_search($topic,$pcount,$p,"8","1","","",$starttime);
			
			}
			else
			{
				$mblog_list = $mblog_get_mblog_from_qq->get_mblog_list_by_search($topic,$pcount,$p,"8","1","",$starttime,time());
			
			}
		
			echo "{$topic}:count:{$mblog_list["data"]["count"]}|get_count".count($mblog_list["data"]["result"]).":p:{$p}<br>\n";
			
			/*$user_info = $mblog_get_mblog_from_qq->get_current_user_info();
			print_r($user_info);*/
			
		
			if($mblog_list["data"]["result"])
			{
				foreach($mblog_list["data"]["result"] as $value1)
				{
						echo "mid:".$value1["id"]."\n";
					//$value1["timestamp"] = strtotime($value1["created_at"]);
					$u_time = $value1["timestamp"];
					if($max_time < $u_time) $max_time = $u_time;
					if($min_time > $u_time || !$min_time)$min_time=$u_time;
					
					
					$love_count = $value1["count"] + $value1["mcount"] ;
					if($love_count < 50)
					{
						echo "love_count:$love_count\n";
						continue;
					}
					
					
					$check_res = true;
				
					if($check_res == true )
					{
						
						$create_time = date("Y-m-d H:i:s",$u_time);
						$date =  date("Y-m-d",$u_time);
						$content = $value1["text"];
						$source =  $value1["from"];
						$nickname = $value1["user"]["name"];
						$qq_user_info = $user_info->get_user_info_by_openid($value1["openid"]);
						
						$uid = $qq_user_info["uid"];
						$mid = $value1["id"] ;
						
						$video_pic=$value1["video"]["picurl"];
						$video_url =$value1["video"]["player"];
						$video_link=$value1["video"]["realurl"];
						$video_title=$value1["video"]["title"];
						
						$from = "1";
						$last_insert_id = $db_mblog_video_content->cron_qq_insert_mblog_video_content($value1["text"],$id,$topic,$value1["bmiddle_pic"],$pic_width_pubu,$pic_height_pubu,$value1["from"],$show_type,$from,$uid,$qq_user_info["user_nickname"],$date,$mid,$create_time,$love_count,$qq_user_info["head_url"],$video_pic,$video_url,$video_link,$video_title);
						
					}
				}
		
			}
				
		}
		
		if($get_type == 2)
		{
			if(!$starttime || ($min_time && $min_time < $starttime))
			{
				$db_cron_get_manage = new db_cron_get_manage();
				$get_res = $db_cron_get_manage->cron_replace_into_data($get_key,"",$min_time);
				
			}
			
		}
		
		if($p == 1)//当为第一页时，检查是否有多页需要抓取
		{	//最大页数为20，接口限制
		
			$max_page = 20;
			$total_page = ceil($mblog_list["data"]["count"]/$pcount);
			$total_page = $total_page > $max_page ? $max_page : $total_page;
	
			if($get_type == 1)
			{
				if($max_time && $max_time > $starttime)
				{
					/*$sql ="update mblog_cron_topic_tag  set cron_start_time = {$max_time} where id ={$id}";
					$db_base->query($sql);*/
					$db_cron_get_manage = new db_cron_get_manage();
					$get_res = $db_cron_get_manage->cron_replace_into_data($get_key,$max_time);
				}
				
				
			}
			
			for($i = 2 ; $i <= $total_page;$i++)
			{
				$this->get_list_by_topic($topic,$id,$i,$starttime,$black_word,$get_key,$get_type);
			}
			
			
		}
			
	}
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
